package com.apes.fn.server.vehicleFile.server.vehicleFileUploadProcess.Process;

import com.apes.fn.server.vehicleFile.model.VehicleFileAccount;
import com.apes.fn.server.vehicleFile.model.VehicleFileAreaItem;
import com.apes.fn.server.vehicleFile.model.VehicleFileUploadInfo;
import com.apes.fn.server.vehicleFile.server.vehicleFileUploadProcess.CommonUploadProcess;
import com.apes.framework.api.SpringManager;
import com.apes.framework.util.MapUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.frame.model.Store;
import com.apes.scm.masterdata.frame.repository.StoreRepository;
import com.apes.scm.masterdata.party.model.Corporate;
import com.apes.scm.masterdata.party.model.PartyRole;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * @program: apes-server
 * @description: 浙江(通用模板)
 * @author: czs
 * @create: 2023年11月8日 09:33:08
 **/
@Service
public class ZhejiangUploadProcess extends CommonUploadProcess {


    @Override
    public Map getTokenMap(VehicleFileAccount vehicleFileAccount) {
        return MapUtil.mapper("companycode",vehicleFileAccount.getAccount(),"companypassword",vehicleFileAccount.getPassword());
    }

    @Override
    public void checkTokenValid(VehicleFileAccount vehicleFileAccount) {
        Date date = new Date();
        if (vehicleFileAccount.getTokenStartDate()==null) {
            vehicleFileAccount.setToken(getToken(vehicleFileAccount));
            vehicleFileAccount.setTokenStartDate(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR,2);
            vehicleFileAccount.setTokenEndDate(calendar.getTime());
        }else if (date.after(vehicleFileAccount.getTokenEndDate())){
            vehicleFileAccount.setToken(getToken(vehicleFileAccount));
            vehicleFileAccount.setTokenStartDate(date);
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.HOUR,2);
            vehicleFileAccount.setTokenEndDate(calendar.getTime());
        }
    }

    @Override
    public Map<String, Object> paket(VehicleFileAccount vehicleFileAccount, VehicleFileUploadInfo vehicleFileUploadInfo) {
        return super.paket(vehicleFileAccount, vehicleFileUploadInfo);
    }

    @Override
    public boolean register(VehicleFileAccount vehicleFileAccount) {
        if(true)return true;
        VehicleFileAreaItem register = vehicleFileAccount.getVehicleFileArea().findItemByApiType("register");
        Dept belongDept = vehicleFileAccount.getBelongDept();
        Store store = SpringManager.getBean(StoreRepository.class).findByDept(belongDept);
        PartyRole company = belongDept.getCompany();
        Corporate corporate = company.getParty().getCorporate();
        if (corporate.getCompanyNature() == null) throw new RuntimeException("缺少公司性质信息，请在菜单【公司】补充公司信息后手动上传");
        if (corporate.getContactPerson() == null) throw new RuntimeException("缺少公司联系人，请在菜单【公司】补充公司信息后手动上传");
        if (corporate.getUscc() == null) throw new RuntimeException("缺少公司统一社会信用代码，请在菜单【公司】补充公司信息后手动上传");
        if (corporate.getMobileNumber() == null) throw new RuntimeException("缺少公司手机号，请在菜单【公司】补充公司信息后手动上传");
        if (corporate.getPostalCode() == null) throw new RuntimeException("缺少公司邮政编码，请在菜单【公司】补充公司信息后手动上传");
        if (store.getAddress() == null) throw new RuntimeException("缺少门店地址，请在菜单【门店信息】补充门店信息后手动上传");
        interfaceData(register.getApiAddress(),
                MapUtil.mapper(
                        "companyname", vehicleFileAccount.getFixCompanyName(),
                        "companypassword", vehicleFileAccount.getPassword(),
                        "companyroadtransportationlicense", "QQQQQQQ",//道路运输经营许可证号
                        "companyunifiedsocialcreditidentifier ", corporate.getUscc(),
                        "companyaddress", store.getAddress(),
                        "companypostalcode", corporate.getPostalCode(),
                        "companyeconomiccategory ", corporate.getCompanyNature().getRemark(),
                        "companycategory", "02",//企业经营业务类别
                        "companylinkmanname", corporate.getContactPerson(),
                        "companylinkmantel", corporate.getMobileNumber(),
                        "companysuperintendentname", corporate.getContactPerson(),//负责人  给的联系人?
                        "companysuperintendenttel", corporate.getMobileNumber(),
                        "companybusinessscope", "02",//企业经营范围
                        "roadtransportationlicensestartdate ", "20200101",//道路经营许可证开始日期, 格式：
                        "roadtransportationlicenseenddate", "20300101",//道路经营许可证结束日期, 格式：
                        "companyoperationstate", "01",
                        "companyadministrativedivisioncode", corporate.getRegProvince().getId(),
                        "companyemail", "1643070326@qq.com"
                )
        );
        return true;
    }
}
